(function($, doc) {
	$.init();
	mui('.mui-scroll-wrapper').scroll({
		scrollY: false
	});

	var vm = new Vue({
		el: '#body',
		data: {
			list: '',
			schoolName: '',
			provinceId: '',
			provinceName: '',
			cityId: '',
			cityName: '',
			regionId: '',
			regionName: '',
			schoolId: '',
			address: '',
			avatar: ''
		}
	});

	common.ajax("student/info", {
		uid: common.uid,
		token: common.token
	}, function(data) {
		if(data.code == 200) {
			Zepto('.checked').eq(data.data.sex - 1).attr("checked", "checked");
			//			console.log(Zepto('.checked').eq(data.data.sex - 1).attr("checked", "checked"));
			//			var address = document.getElementsByClassName("address")[0]
			//			var schools = document.getElementsByClassName("schools")[0];
			//			var city=document.getElementById("showCityPicker3");
			//			console.log(city.innerHTML);
			vm.list = data.data;
			var tmp = data.data;
			//			address.setAttribute("fkProvinceId", data.data.fkProvinceId)
			//			address.setAttribute("fkCityId", data.data.fkCityId)
			//			address.setAttribute("fkRegionId", data.data.fkRegionId)
			//			address.setAttribute("fkProvincename", data.data.provinceName)
			//			address.setAttribute("fkCityname", data.data.cityName)
			//			address.setAttribute("fkRegionname", data.data.regionName)
			//			schools.setAttribute("name", data.data.schoolName)
			//			schools.setAttribute("uid", data.data.fkSchoolId)

			provinceId = tmp.fkProvinceId;
			provinceName = tmp.provinceName;
			cityId = tmp.fkCityId;
			cityName = tmp.cityName;
			regionId = tmp.fkRegionId;
			regionName = tmp.regionName;
			schoolId = tmp.fkSchoolId;
			address = tmp.address;
			schoolName = tmp.schoolName;
			cardId = tmp.cardId;
			avatar = tmp.avatar;
		} else {
			mui.toast(tmp);
		}
	}, "get");
	var cityPicker = document.getElementById("showCityPicker3").innerText
	$.ready(function() {
		var _getParam = function(obj, param) {
			return obj[param] || '';
		};
   
		var bb = new $.PopPicker();
		bb.setData([{
			text: '一年级'
		}, {
			text: '二年级'
		}, {
			text: '三年级'
		}, {
			text: '四年级'
		}, {
			text: '五年级'
		}, {
			text: '六年级'
		}]);
		var aa = doc.getElementById('aa');
		var newcls = doc.getElementById('newcls')
		aa.addEventListener('tap', function(event) {
			bb.show(function(items) {
				newcls.innerText = items[0].text;
			});

		}, false);
		var user = new $.PopPicker();
		user.setData([{
			text: '1班'
		}, {
			text: '2班'
		}, {
			text: '3班'
		}, {
			text: '4班'
		}, {
			text: '5班'
		}, {
			text: '6班'
		}]);
		var classes = doc.getElementById('classes');
		var cls = doc.getElementById("classname")
		classes.addEventListener('tap', function(event) {
			user.show(function(items) {
				cls.innerText = items[0].text;

			});

		}, false);

		var userPicker = new $.PopPicker();
		var cityPicker3 = new $.PopPicker({
			layer: 3
		});
		var school = []
		cityPicker3.setData(cityData3);
		var showCityPickerButton = doc.getElementById('showCityPicker3');
		var city = doc.getElementById('city');
		showCityPickerButton.addEventListener('tap', function(event) {
			school = []
			cityPicker3.show(function(items) {
				showCityPicker3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
				if(items[2].value == "") {
					mui.toast("出现未知错误,请重新选择")
					return
				}
				showCityPickerButton.setAttribute("fkProvinceId", items[0].value)
				showCityPickerButton.setAttribute("fkCityId", items[1].value)
				showCityPickerButton.setAttribute("fkRegionId", items[2].value)
				showCityPickerButton.setAttribute("fkProvincename", items[0].text)
				showCityPickerButton.setAttribute("fkCityname", items[1].text)
				showCityPickerButton.setAttribute("fkRegionname", items[2].text)
				var texts = showCityPickerButton.getAttribute("fkProvincename") + showCityPickerButton.getAttribute("fkCityname") + showCityPickerButton.getAttribute("fkRegionname");
				var schollss = document.getElementById("showUserPicker");
				console.log(texts);
				if(texts != undefined) {
					console.log(1111);
					schollss.innerHTML == "000";
				}
				showCityPickerButton.innerHTML = texts;
				//console.log(texts);
				common.ajax("school/getSchool", {
					uid: common.uid,
					regionId: items[2].value
				}, function(data) {
					if(data.code == 200) {
						for(var i = 0; i < data.data.length; i++) {
							school.push({
								text: data.data[i].schoolName,
								id: data.data[i].id
							})
						}
						if(school != "") {
							document.getElementById("showUserPicker").style.color = "#333"
						} else {
							document.getElementById("showUserPicker").style.color = "#B2B2B2"
							document.getElementById("showUserPicker").innerText = '请输入学校名称'
						}
						userPicker.setData(school)
					} else {
						mui.toast(data.message)
					}
				})
			});
		}, false);

		var showUserPickerButton = doc.getElementById('showUserPicker');
		showUserPickerButton.addEventListener('tap', function(event) {
			if(document.getElementById("showCityPicker3").innerText == cityPicker) {
				mui.toast("地区不能为空!!!")
				return
			}
			if(school == "") {
				mui.toast("当前地区尚未开放!!!")
				return
			}
			userPicker.show(function(items) {
				showUserPickerButton.innerText = items[0].text;
				showUserPickerButton.setAttribute("name", items[0].text)
				showUserPickerButton.setAttribute("uid", items[0].id)
			});
		}, false);

	});
})(mui, document);

common.focu(".mui-input-clear", "green")

var save = document.getElementById('save');
var file = document.getElementById('file');
var back = document.getElementById('back');
var body = document.getElementById("body")
file.onclick = function() {
	body.style.display = "none"
	var imgBox = document.getElementById('imgBox');
	document.getElementsByTagName('body')[0].style.backgroundColor = '#000';
	imgBox.style.display = 'block';
	file.style.display = "none"
};
save.onclick = function() {
	imgBox.style.display = 'none';
	body.style.display = "block"
	document.getElementsByTagName('body')[0].style.backgroundColor = '#f6f6f6';
	file.style.display = "block"
}
back.onclick = function() {
	plus.webview.currentWebview().reload();
}
$(".append").on("tap", function() {
	plus.gallery.pick(function(path) {
		$(".append").html("<div><img src='" + path + "'/></div>")
	}, function(e) {
		mui.toast("你取消了选择图片!");
	}, {
		filter: "image"
	});
})
var base64;
var cut = new ImgClip({
	canvas: 'canvas01', // canvas id
	fileObj: 'file', // file id
	cutBtn: 'save', // cut btn id
	resultObj: 'img', // result img i
	rotateR: 'rotateR',
	cutScale: 1, // 1:1、3:4
	cutW: '300' // '数字'或者'winW'关键字，裁切宽度，随屏幕宽或自己设置
});

var showCityPicker3 = $("#showCityPicker3").text()
var schol = $("#showUserPicker").text()

$(".next").on("tap", ".next-button", function() {
	var provinceIds = Zepto("#showCityPicker3").attr("fkProvinceId") ? Zepto("#showCityPicker3").attr("fkProvinceId") : provinceId;
	var provinceNames = Zepto("#showCityPicker3").attr("fkprovincename") ? Zepto("#showCityPicker3").attr("fkprovincename") : provinceName;
	var cityIds = Zepto("#showCityPicker3").attr("fkCityId") ? Zepto("#showCityPicker3").attr("fkCityId") : cityId;
	var cityNames = Zepto("#showCityPicker3").attr("fkcityname") ? Zepto("#showCityPicker3").attr("fkcityname") : cityName;
	var regionIds = Zepto("#showCityPicker3").attr("fkRegionId") ? Zepto("#showCityPicker3").attr("fkRegionId") : regionId;
	var regionNames = Zepto("#showCityPicker3").attr("fkregionname") ? Zepto("#showCityPicker3").attr("fkregionname") : regionName;
	var schoolIds = Zepto("#showUserPicker").attr("uid") ? Zepto("#showUserPicker").attr("uid") : schoolId;
	var schoolNames = Zepto("#showUserPicker").attr("name") ? Zepto("#showUserPicker").attr("name") : schoolName;
	var classNames = Zepto("#classname").text() ? Zepto("#classname").text() : className;
	var addresss = (Zepto("#showCityPicker3").attr("fkprovincename") + Zepto("#showCityPicker3").attr("fkcityname") + Zepto("#showCityPicker3").attr("fkregionname")) ? (Zepto("#showCityPicker3").attr("fkprovincename") + Zepto("#showCityPicker3").attr("fkcityname") + Zepto("#showCityPicker3").attr("fkregionname")) : address;
	//	var avatars=Zepto("#img").attr("src")?Zepto("#img").attr("src"):avatar;
	var avatars = ''
	if(avatar != $("#img")[0].currentSrc) {
		avatars = $("#img")[0].currentSrc
	}
	console.log(address);
	console.log(addresss);
	if($(".studentname").val() == "") {
		$(".textprompt").css("display", "block")
		mui.toast("姓名不能为空")
		return
	} else if(showCityPicker3 == $("#showCityPicker3").text()) {
		mui.toast("请选择地区!!!")
		return
	} else if(addresss != address && schoolNames == schoolName) {
		address = addresss;
		mui.toast("地区已更换，请重新选择学校!!!")
		return
	}

	/*获取男女老师*/
	var gender = null
	Zepto.each(Zepto(".checked"), function(a, e) {
		if(Zepto(".checked")[a].checked) {
			gender = Zepto(this).index()
		}
	});

	//	console.log($("#img")[0].currentSrc);
	//console.log(avatars);
	/*
	console.log(provinceIds);
	console.log(provinceNames);
	console.log(cityIds);
	console.log(cityNames);
	console.log(regionIds);
	console.log(regionNames);
	console.log(schoolIds);
	console.log(schoolNames);
	console.log(classNames);
	console.log(addresss);*/

	common.ajax("student/modifyInformation", {
		token: common.token,
		uid: common.uid,
		sex: gender,
		name: $(".studentname").val(), //名字
		grade: $("#newcls").text(),
		//provinceId: $("#showCityPicker3").attr("fkProvinceId"), //省份id
		provinceId: provinceIds, //省份id
		//provinceName: $("#showCityPicker3").attr("fkprovincename"), //省名字
		provinceName: provinceNames,
		cityId: cityIds, //城市id
		cityName: cityNames, //城市名字
		regionId: regionIds,
		regionName: regionNames,
		//schoolId: $("#showUserPicker").attr("uid"), //学校名字
		//schoolId:schoolId,
		schoolId: schoolIds,
		//schoolName:$("#showUserPicker").attr("name"),
		schoolName: schoolNames,
		//classId: $("#showUserPicker").attr("uid"), //班级id
		className: classNames,
		//cardId: $("#cardId").val(), //学籍编号
		cardId: cardId,
		address: addresss,
		avatar: avatars, //头像
	}, function(data) {
		if(data.code == 200) {
			var list = plus.webview.currentWebview("my.html").opener();　　　　 //触发父页面的自定义事件(refresh),从而进行刷新
			mui.fire(list, 'refresh');
			mui.toast("修改成功");
			mui.back();
		} else {
			mui.toast(data.message)
		}
	})
})